package com.wenlin.myproduction.ui;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.wenlin.myproduction.R;
import com.wenlin.myproduction.base.BaseDialogFragment;
import com.wenlin.myproduction.bean.Empty;
import com.wenlin.myproduction.bean.RosterSelectBean;
import com.wenlin.myproduction.bean.RosterSelectWithChangeBean;
import com.wenlin.myproduction.binder.EmptyBinder;
import com.wenlin.myproduction.binder.RosterSelectBinder;
import com.wenlin.myproduction.util.Checker;
import com.wenlin.myproduction.util.RosterUtil;
import com.wenlin.myproduction.util.ToastUtils;
import com.wenlin.myproduction.util.TypeData;
import com.wenlin.myproduction.util.TypeDataNames;
import com.wenlin.myproduction.util.UI;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import me.drakeet.multitype.Items;
import me.drakeet.multitype.MultiTypeAdapter;

/**
 * @author 文琳_377979485@qq.com
 * @time 2021/10/15 0015 下午 5:00
 * @desc 选择班次弹窗
 */
public class RosterSelectDialog extends BaseDialogFragment {
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.rv)
    RecyclerView rv;

    private Items items = new Items();
    private MultiTypeAdapter adapter;
    private boolean changeChecked;
    private RosterSelectBean selectedBean;


    public RosterSelectDialog getDialog(Activity activity) {
        Fragment fragment = getFragment(activity, RosterSelectDialog.class.getName());
        RosterSelectDialog dialog = (RosterSelectDialog) fragment;
        dialog.setStyle(DialogFragment.STYLE_NO_TITLE, R.style.ImageSaveDialog);
        return dialog;
    }

    public RosterSelectDialog show(Activity activity) {
        if (getShow(activity, false))
            show(getFm(activity), RosterSelectDialog.class.getName());
        return this;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_roster_select;
    }

    @Override
    protected void initView(View view, Bundle savedInstanceState) {
        UI.setText(tvTitle, "班次选择");
        adapter = new MultiTypeAdapter(items);
        adapter.register(RosterSelectBean.class, new RosterSelectBinder());
        adapter.register(Empty.class, new EmptyBinder());
        rv.setLayoutManager(new GridLayoutManager(getContext(), 1));
        rv.setHasFixedSize(true);
        rv.setAdapter(adapter);
        getData();
    }

    /**
     * 获取数据
     */
    private void getData() {
        List<RosterSelectBean> list = RosterUtil.getList();
        if (Checker.hasList(items)) {
            items.clear();
            adapter.notifyDataSetChanged();
        }
        if (Checker.hasList(list)) {
            items.addAll(list);
            adapter.notifyDataSetChanged();
            selectedBean = null;
        } else if (items.size() == 0) {
            items.add(new Empty());
            adapter.notifyDataSetChanged();
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void threadHandle(TypeData typeData) {
        String name = typeData.getName();
        if (name.equals(TypeDataNames.clickItemRosterSelect)) {
            RosterSelectBean bean = (RosterSelectBean) typeData.getData();
            if (Checker.isNull(bean)) return;
            if (Checker.hasList(items)) {
                for (int i = 0; i < items.size(); i++) {
                    Object o = items.get(i);
                    if (o instanceof RosterSelectBean) {
                        RosterSelectBean b = (RosterSelectBean) o;
                        if (b.getId().equals(bean.getId())) {
                            b.setSelected(true);
                            selectedBean = b;
                        } else {
                            b.setSelected(false);
                        }
                    }
                }
                adapter.notifyDataSetChanged();
            }
        }
    }

    @OnClick({R.id.btn})
    void click(View view) {
        int id = view.getId();
        if (id == R.id.btn) {
            if (Checker.isNull(selectedBean)) {
                ToastUtils.getInstance().showInfo("请选择一个班次");
                return;
            }
            EventBus.getDefault().post(new TypeData<>(TypeDataNames.setScheduleRosterSelectBean, new RosterSelectWithChangeBean(selectedBean, changeChecked)));
            dismiss();
        }
    }
}
